package com.seeyon.ctp.rest.resources;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.apache.commons.lang.StringUtils;

import com.seeyon.cap4.form.bean.FormBean;
import com.seeyon.cap4.form.bean.FormDataMasterBean;
import com.seeyon.cap4.form.bean.FormFieldBean;
import com.seeyon.cap4.form.bean.FormFieldComEnum;
import com.seeyon.cap4.form.bean.FormTableBean;
import com.seeyon.cap4.form.bean.fieldCtrl.FormFieldCtrl;
import com.seeyon.cap4.form.service.CAP4FormManager;
import com.seeyon.cap4.template.service.CAPCustomService;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.exceptions.BusinessException;

/**
 * Created by 
 * 自定义控件--链接绑定
 */
@Path("cap4/BindUrlControl")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class BindUrlResourcces extends BaseResource{
	
	@GET
	@Path("getBindFields")
	@Produces(MediaType.APPLICATION_JSON)
	public Response  getBindFields(@QueryParam("formId") String formId, @QueryParam("currentFieldName") String currentFieldName,@QueryParam("currentFormName") String currentFormName) {
		CAP4FormManager cap4FormManager = (CAP4FormManager) AppContext.getBean("cap4FormManager");
        FormBean formBean = cap4FormManager.getEditingForm(Long.valueOf(formId));
        List<FormFieldBean> masterFields = formBean.getMasterTableBean().getFields();
        List<Map<String, Object>> resList = new ArrayList<Map<String, Object>>();
        for (FormFieldBean field : masterFields) {
            //排出当前字段名称
        	FormFieldComEnum currentFieldType = field.getInputTypeEnum();
            if (currentFieldType!=null&&(currentFieldType==FormFieldComEnum.TEXT||(currentFieldType==FormFieldComEnum.TEXTAREA&&"VARCHAR".equals(field.getFieldType())))) {
            	FormFieldCtrl ctrl = field.getFieldCtrl();
            	Map<String, Object> info = 	field.getJsonObj4Design(false);
            	info.put("tableName", "主表");
            	resList.add(info);
            }
        } 
        //如果为重复表的字段，则把该重复表的添加进去
        if (!formBean.getMasterTableBean().getTableName().equals(currentFormName)) {
        	 List<FormTableBean> subTableBeans =  formBean.getSubTableBean();
        	 FormTableBean subTableBean = null;
        	 for (FormTableBean formTableBean : subTableBeans) {
        		 if (formTableBean.getFieldBeanByName(currentFieldName)!=null) {
        			 subTableBean = formTableBean;
        			 break;
				}
			 }
        	 if (subTableBean!=null) {
        		 List<FormFieldBean> subFileds = subTableBean.getFields();
        		 for (FormFieldBean formFieldBean : subFileds) {
        			 FormFieldComEnum currentFieldType = formFieldBean.getInputTypeEnum();
        			 if (currentFieldType!=null&&(currentFieldType==FormFieldComEnum.TEXT||(currentFieldType==FormFieldComEnum.TEXTAREA&&"VARCHAR".equals(formFieldBean.getFieldType())))) {
        				 FormFieldCtrl ctrl = formFieldBean.getFieldCtrl();
        				 Map<String, Object> info = 	formFieldBean.getJsonObj4Design(false);
        				 info.put("tableName", subTableBean.getDisplay());
        				 resList.add(info);
        			 }
        		 }
        	 }
        	
		}
       
        return success(resList);
	}
	/**
	 * @param formId 表单的id
	 * @param masterId 主表的id
	 * @param fieldName 映射的字段名称
	 * @param formName  映射的字段的所在表
	 * @param isSubField 是否是从表字段
	 * @param subRecordId 从表字段id
	 * @return
	 */
	@POST 
	@Path("getRequestURL")
	@Produces(MediaType.APPLICATION_JSON)
	public Response  getRequestURL(Map<String,Object> map) {
 		Map mergeDataMap = (Map) map.get("data");
		String formId = (String) map.get("formId");
		String masterId = (String) map.get("masterId");
		String fieldName = (String) map.get("fieldName");
		String formName = (String) map.get("formName");
		String subRecordId = (String) map.get("subRecordId");
		//Boolean isSubField = (Boolean) map.get("isSubField");
		CAP4FormManager cap4FormManager = (CAP4FormManager) AppContext.getBean("cap4FormManager");
		FormBean formBean = cap4FormManager.getForm(Long.valueOf(formId), false);
		FormDataMasterBean dataMasterBean =	cap4FormManager.getSessioMasterDataBean(Long.valueOf(masterId));
		try {
			if (dataMasterBean == null) {
				dataMasterBean = cap4FormManager.getDataMasterBeanById(Long.valueOf(masterId), formBean, null);
			}else {
				 CAPCustomService capCustomService = (CAPCustomService) AppContext.getBean("capCustomService");
				 if (mergeDataMap!=null) {
						capCustomService.mergeFormData(formBean, dataMasterBean, mergeDataMap);
				}
			}
		 } catch (Exception e) {
		}
		Map<String, Object> result = new HashMap();
		try {
			result.put("url", "");
			if (dataMasterBean==null) {
				result.put("state", false);
				result.put("msg", "未找到缓存数据");
		        return success(result);
			}
		} catch (Exception e) {
			result.put("state", false);
			result.put("msg", "未找到缓存数据");
	        return success(result);
		}
		if (StringUtils.isNotBlank(subRecordId)) {
			Map<String, Object>  data = dataMasterBean.getSubDataMapById(formName, Long.valueOf(subRecordId));
			String url = (String) data.get(fieldName);
			result.put("url", url);
		}else {
			String url = (String) dataMasterBean.getFieldValue(fieldName);
			result.put("url", url);
		}
        return success(result);
	}
}
